import { Effect, ImmerReducer, Reducer, Subscription } from 'umi';
import { gettags,getswiper ,getArticlelist,readingList,getlabel} from "@/services";
import {listItem, tagslistItem,labelItem}  from "@/utils/Article"
export interface IndexModelState {
  tagslist: Array<tagslistItem>
  swiperlist:Array<listItem>,
  articlelist:Array<listItem>,
  readList:Array<listItem>,
  labellist:Array<labelItem>
}

export interface IndexModelType {
  namespace: 'Article';
  state: IndexModelState;
  effects: {
    gettags: Effect,
    getswiper: Effect;
    getArticlelist: Effect;
    readinglist:Effect;
    getlabel:Effect
  };
  reducers: {
    save: Reducer<IndexModelState>;
    // 启用 immer 之后
    // save: ImmerReducer<IndexModelState>;
  };
  subscriptions?: { setup: Subscription };
}

const IndexModel: IndexModelType = {
  namespace: 'Article',

  state: {
    tagslist: [],
    swiperlist:[],
    articlelist:[],
    readList:[],
    labellist:[]
  },

  effects: {
    //获取标签
    *gettags({ payload }, { call, put }) {
        let res =yield call(gettags,100,200)
      yield put({
            type:"save",
            payload:{
              tagslist:res.data
            }
        })
    },
    //获取轮播图数据
    *getswiper({ payload }, { call, put }) {
      let res =yield call(getswiper,100,200)
      
    yield put({
          type:"save",
          payload:{
              swiperlist:res.data
          }
      })
     
  },
  *getArticlelist({ payload }, { call, put }) {
    let res =yield call(getArticlelist,100,200)
  yield put({
        type:"save",
        payload:{
            articlelist:res.data[0]
        }
    })
},
*readinglist({payload},{call,put}){
  let result=yield call(readingList)
  yield put({
    type:'save',
    payload:{
      readList:result.data
    }
  })
},
*getlabel({payload},{call,put}){
  let result=yield call(getlabel)
  yield put({
    type:'save',
    payload:{
      labellist:result.data
    }
  })
},
  },
  reducers: {
    save(state, action) {
      return {
        ...state,
        ...action.payload,
      };
    },
    // 启用 immer 之后
    // save(state, action) {
    //   state.name = action.payload;
    // },
  },
 
};

export default IndexModel;